package deployr.cli;

import deployr.core.*;
import deployr.domain.*;

public class CreateProjectWebppResponder extends Responder {

	private final Projects projects;
	private final Workspace workspace;

	public CreateProjectWebppResponder(Workspace workspace, Projects projects) {
		this.workspace = workspace;
		this.projects = projects;
	}
	
	@Override
	protected boolean shouldRespond(String method, String requestURI) {
		return "POST".equals(method) && requestURI.matches(".*/projects/.*/webapp.*");
	}	

	@Override
	protected Result respondTo(String method, String requestURI, Input input) {

		String projectKey = requestURI.replaceAll(".*/projects/", "").replaceAll("/webapp.*", "");
		
		IProject project = projects.select(projectKey);
		if(notFound(project)) return new Result(404);
		Deploy deploy = new Deploy(workspace.getAssets(), project);
		deploy.webappTo(workspace.getWebapps());

		return new Result(200);
	}
}
